package com.celan.year2023.month05.day29;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @author Celan
 * @description TODO
 */
public class Solution {
    public boolean wordPattern(String pattern, String s) {
        //<pattern,word>
        HashMap<Character, String> dic = new HashMap<>();
        HashSet<String> wordSet = new HashSet<>();
        HashSet<Character> patternSet = new HashSet<>();
        String[] split = s.split(" ");
        int n = pattern.length();
        if (n != split.length) return false;

        for (int i = 0; i < n; i++) {
            char c = pattern.charAt(i);
            String word = split[i];
            wordSet.add(word);
            patternSet.add(c);
            if (dic.containsKey(c) && !dic.get(c).equals(word)) {
                return false;
            }
            if (!dic.containsKey(c)) {
                dic.put(c, word);
            }
        }
        return wordSet.size() == patternSet.size();
    }
}
